package com.sdkj.recruit.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.ILabelDao;
import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.model.Label;
import com.sdkj.recruit.model.Paginate;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.util.StringUtil;

@Persistent
@Service("labelDao")
public class LabelDao implements ILabelDao {

	@Autowired
	private IMongodbDAO dao;
	
	@Override
	public void saveLabel(Label label) {
		dao.saveOrUpdatePO(label);
	}

	@Override
	public void deleteLabel(String labelId) {
		dao.deletePOList(Label.class, QueryParamCreator.eq("labelId", labelId));
	}

	@Override
	public long queryAllLabelCount(String keyWord) {
		
		List<Criteria> condList = new ArrayList<Criteria>();
		if(!StringUtil.isEmptyString(keyWord)){
			condList.add(QueryParamCreator.like("labelName", keyWord));
		}
		return dao.getRowCount(Label.class, (Criteria[]) condList.toArray(new Criteria[0]));
	}

	@Override
	public List<Label> queryLabelList(int pageNo, int pageSize, String keyWord) {
		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "createDate");
		orders.add(order);
		
		List<Criteria> condList = new ArrayList<Criteria>();
		
		if(!StringUtil.isEmptyString(keyWord)){
			condList.add(QueryParamCreator.like("labelName", keyWord));
		}
		condList.add(QueryParamCreator.isNotEmpty("labelId"));
		return dao.getPOList(Label.class, new Paginate(pageNo, pageSize), orders, (Criteria[]) condList.toArray(new Criteria[0]));
	}

	@Override
	public Label queryLableByName(String labelName) {
		return dao.getPO(Label.class, QueryParamCreator.eq("labelName", labelName));
	}

	@Override
	public void deleteLabelByComapanyId(String companyId) {
		dao.deletePOList(Label.class, QueryParamCreator.eq("companyId", companyId));
	}

	@Override
	public void saveLabelList(List<Label> list) {
		dao.saveOrUpdatePOList(list);
	}

	@Override
	public List<Label> queryLableByCompanyId(String companyId) {
		return dao.getPOList(Label.class, QueryParamCreator.eq("companyId", companyId));
	}

}
